<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;

class Index extends Frontend
{

    protected $noNeedLogin = 'login,index';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        $slide = Db::name("slide")->where('status',1)->order('sort desc')->select();
        $notice = Db::name("notice")->where('status',1)->order('sort desc')->find();
        $goodsList = Db::name("goods")->alias('g')
            ->join('goods_type got','got.id=g.goods_type','left')
            ->join('games_type get','get.id=g.game_id','left')
            ->join('user u','u.id=g.user_id','left')
            ->field('g.*,got.name as goname,get.name as gename,get.image as image,u.nickname as nickname')
            ->where('g.status',1)->order('g.id desc')->select();

        $this->assign('goodsList',$goodsList);
        $this->assign('notice',$notice);
        $this->assign('slide',$slide);
        return $this->view->fetch();
    }


    /**
     * 发单
     */
    public function release()
    {
        header("Content-type:text/html;charset=utf-8");
        $uid = $this->auth->id;
        if ($this->request->isPost()) {
            if($_POST['game_id'] == "王者荣耀"){
                $game_id = 1;
            }
            if($_POST['dl_ask'] == ''){
                $_POST['dl_ask'] = "无";
            }
            if($_POST['other'] == ''){
                $_POST['other'] = "无";
            }
            if($_POST['mwgrade'] == ''){
                $_POST['mwgrade'] = "0";
            }
            if($_POST['heroes'] == ''){
                $_POST['heroes'] = "0";
            }
            $no = intval(time()/60);
            $arrDate = [
//                'title' => $_POST['title'],
                'dl_price' => $_POST['dl_price'],
                'user_id' => $uid,
                'ask_ok_time' => $_POST['ask_ok_time'],
                'game_id' => $game_id,
                'goods_type' => $_POST['goods_type'],
                'client' => $_POST['client'],
                'fu' => '/',
                'qu' => $_POST['qu'],
                'current_rank' => $_POST['xz_rank'].'/'.$_POST['xz_star'],
                'target_rank' => $_POST['mb_rank'].'/'.$_POST['mb_star'],
                'bond' => $_POST['bond'],
                'game_account' => $_POST['game_account'],
                'game_pass' => $_POST['game_pass'],
                'mwgrade' => $_POST['mwgrade'],
                'heroes' => $_POST['heroes'],
                'moblie' => $_POST['moblie'],
                'dl_ask' => $_POST['dl_ask'],
                'other' => $_POST['other'],
                'order' => 'DL'.date("Ymd").$no.$_POST['dl_price'],
                'status' => 1,
//                'addtime' => time(),
            ];
            if($arrDate['dl_price'] == '' || $arrDate['ask_ok_time'] == ''
                || $arrDate['bond'] == '' || $arrDate['game_account'] == '' || $arrDate['game_pass'] == ''
                || $arrDate['game_pass'] == '' || $arrDate['dl_ask'] == '' || $arrDate['mwgrade'] == ''){
                $jsonarr = [
                    "msg" => '必填项不能为空',
                    "code" => '100'
                ];
                return $jsonarr;
            }else{
                $res = Db::name("goods")->insert($arrDate);
                if($res){
                    $jsonarr = [
                        "msg" => '请继续下一步',
                        "code" => '101'
                    ];
                    return $jsonarr;
                }
            }
        }

        $gameList = Db::name("games_type")->where('status',1)->order('sort desc')->select();//游戏列表

        $client = Db::name("games_client")->where('status',1)->order('sort desc')->select();//客户端  安卓/苹果

        $qu = Db::name("games_qufu")->where('game_id',1)->where('status',1)->find();//区列表
        $qudata = json_decode($qu['name'],true);//解析json数据

        $rank = Db::name("games_rank")->where('game_id',1)->where('status',1)->find();//段位列表
        $rankdata = json_decode($rank['name'],true);//解析json数据

        $goodtype = Db::name("goods_type")->where('id',1)->select();//订单类型

        $this->assign('qu',$qudata['name']);
        $this->assign('rank',$rankdata['name']);
        $this->assign('star',$rankdata['star']);
        $this->assign('goodtype',$goodtype);
        $this->assign('game',$gameList);
        $this->assign('client',$client);
        return $this->view->fetch();
    }

    /**
     * 发单详情页
     */
    public function release_info()
    {
        if (!empty($_GET)) {
            $good_id = $_GET["good_id"];
            $goodsList = Db::name("goods")->where('id',$good_id)->find();
            $user = Db::name("user")->where('id',$goodsList['user_id'])->find();
//            if($goodsList['status'] != 1){
//                $jsonarr = [
//                    "msg" => '该订单不属于你哦',
//                    "code" => '1'
//                ];
//                return $jsonarr;
//            }
        }else{
            $this->error('订单错误！','index/index');
        }
        $this->assign('user',$user);
        $this->assign('goods',$goodsList);
        return $this->view->fetch();
    }

//    /**
//     * 接单事件
//     */
//    public function rec_order()
//    {
//        if (!empty($_GET)) {
//            $uid = $this->auth->id;
//            $bondmoney = $_GET["bondmoney"];//保证金
//            $user = Db::name("user")->where('id',$uid)->find();
//            if($user['real_name'] == NULL && $user['id_card_no'] == NULL){
//                $jsonarr = [
//                    "msg" => '还未实名认证哦',
//                    "code" => '0'
//                ];
//                return $jsonarr;
//            }elseif($user['money'] < $bondmoney){
//                $jsonarr = [
//                    "msg" => '余额不足，请充值',
//                    "code" => '1'
//                ];
//                return $jsonarr;
//            }else{
//                $jsonarr = [
//                    "msg" => '正在接单中',
//                    "code" => '2'
//                ];
//                return $jsonarr;
//            }
//        }else{
//            $this->error('订单错误！','index/index');
//        }
//
//        return $this->view->fetch();
//    }

    /**
     * 接单事件
     */
    public function rec_orders()
    {
        if (!empty($_GET)) {
            $uid = $this->auth->id;
            $order_no = Db::name("goods")->where('id',$_GET["goodid"])->find();
            $bondmoney = $_GET["bondmoney"];//保证金
            $goodprice = $_GET["goodprice"];//代练价格
            $goodid = $_GET["goodid"];//商品od
            $order_no = $order_no['order'];//订单编号
            $fa_uid = $_GET["fa_uid"];//发单用户
            $user = Db::name("user")->where('id',$uid)->find();
            if($user['real_name'] == NULL && $user['id_card_no'] == NULL){
                $jsonarr = [
                    "msg" => '还未实名认证哦',
                    "code" => '0'
                ];
                return $jsonarr;
            }elseif($user['money'] < $bondmoney){
                $jsonarr = [
                    "msg" => '余额不足，请充值',
                    "code" => '1'
                ];
                return $jsonarr;
            }else{
                $arrDate = [
                    'good_id' => $goodid,
                    'order_no' => $order_no,
                    'status' => '2',
                    'fa_user_id' => $fa_uid,
                    'jie_user_id' => $uid,
                ];
                $res = Db::name("orders")->insert($arrDate);
                if($res){
                    $now = $user['money'] - $bondmoney;
                    $up_money = Db::name("user")->where('id',$uid)->update(['money'=>$now]);
                    $up_goods = Db::name("goods")->where('id',$goodid)->update(['status'=>'2']);
                    $insDate = [
                        'user_id' => $uid,
                        'content' => '接单扣除保证金',
                        'money' => $bondmoney,
                        'good_id' => $goodid,
                    ];
                    $money_log = Db::name("money_log")->insert($insDate);
                    $jsonarr = [
                        "msg" => '已接单',
                        "code" => '2'
                    ];
                    return $jsonarr;
                }else{
                    $jsonarr = [
                        "msg" => '错误',
                        "code" => '3'
                    ];
                    return $jsonarr;
                }
            }
        }else{
            $this->error('订单错误！','index/index');
        }

//        return $this->view->fetch();
    }

    /**
     * 订单列表
     */
    function order_list(){
        $uid = $this->auth->id;
        $order_list = Db::name("orders")->alias('or') //所有订单
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->order('or.id DESC')
            ->select();
        $dailianzhong = Db::name("orders")->alias('or') //代练中 2
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','2')
            ->order('or.id DESC')
            ->select();
        $daiyanshou = Db::name("orders")->alias('or') //待验收 3
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','3')
            ->order('or.id DESC')
            ->select();
        $yiwancheng = Db::name("orders")->alias('or') //已完成 4
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','4')
            ->order('or.id DESC')
            ->select();
        $yixiajia = Db::name("orders")->alias('or') //已下架 5
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','5')
            ->order('or.id DESC')
            ->select();
        $yishanchu = Db::name("orders")->alias('or') //已删除 6
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','6')
            ->order('or.id DESC')
            ->select();
        $yichexiao = Db::name("orders")->alias('or') //已撤销 7
            ->join('user u','u.id = or.fa_user_id','left')
            ->join('goods g','g.order = or.order_no','left')
            ->field('or.status as status,or.good_id as good_id,u.nickname as nickname,g.goods_type as goods_type,
            g.client as client,g.qu as qu,g.bond as bond,g.dl_price as dl_price,g.game_account as game_account,g.ask_ok_time as ask_ok_time')
            ->where('or.jie_user_id',$uid)
            ->where('or.status','7')
            ->order('or.id DESC')
            ->select();
        $this->assign('order_list',$order_list);
        $this->assign('dailianzhong',$dailianzhong);
        $this->assign('daiyanshou',$daiyanshou);
        $this->assign('yiwancheng',$yiwancheng);
        $this->assign('yixiajia',$yixiajia);
        $this->assign('yishanchu',$yishanchu);
        $this->assign('yichexiao',$yichexiao);
        return $this->view->fetch();
    }

    /**
     * 订单详情
     */
    function order_details(){
        if (!empty($_GET)) {
            $goodid = $_GET['goodid'];
            $fa_user = $_GET['fa_user'];
            $goods_list = Db::name('goods')->where('id',$goodid)->find();
            $order_list = Db::name('orders')->where('good_id',$goodid)->find();
            $this->assign('goods',$goods_list);
            $this->assign('order',$order_list);
            $this->assign('fa_user',$fa_user);
        }else{
            $this->error('订单错误！','index/index');
        }
        return $this->view->fetch();
    }
}
